package com.hkj.configcenter.admin.controller;

import com.hkj.configcenter.admin.url.PageUrl;
import com.hkj.configcenter.admin.url.ServiceUrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@Controller
public class LoginController {

    public static String LOGIN_NAME;

    public static String LOGIN_PASSWORD;

    @Value("${login.name}")
    public void setLoginName(String loginName) {
        LOGIN_NAME = loginName;
    }

    @Value("${login.password}")
    public void setLoginPassword(String loginPassword) {
        LOGIN_PASSWORD = loginPassword;
    }

    @GetMapping(ServiceUrl.HOME)
    public String home(HttpSession session) {

        if (session.getAttribute(LoginController.LOGIN_NAME) == null) {
            return PageUrl.LOGIN_PAGE;
        }

        return "redirect:" + ServiceUrl.CONFIG_CENTER_HOME;
    }

    @PostMapping("/login")
    public String login(@RequestParam("name") String name,
                        @RequestParam("password") String password,
                        HttpServletRequest request, HttpSession session) {
        String ip = getIpAddress(request);
        log.info("【配置中心登录】【{}】name={}, password={}", ip, name, password);

        if (!LOGIN_NAME.equals(name)) {
            log.info("【配置中心登录】【{}】username is not exists", ip);
            return "username or password is not exists";
        }

        if (!LOGIN_PASSWORD.equals(password)) {
            log.info("【配置中心登录】【{}】password is not exists", ip);
            return "username or password is not exists";
        }

        session.setAttribute(LOGIN_NAME, LOGIN_PASSWORD);
        session.setMaxInactiveInterval(3 * 60 * 60);
        log.info("【配置中心登录】【{}】登录成功", ip);
        return "redirect:" + ServiceUrl.CONFIG_CENTER_HOME;
    }

    @ResponseBody
    @PostMapping("/loginOut")
    public String loginOut(HttpServletRequest request) {
        String ip = getIpAddress(request);
        log.info("【配置中心登录】【{}】注销", ip);
        request.getSession().invalidate();
        return "success";
    }

    @GetMapping("/loginOut")
    public String loginout(HttpServletRequest request) {
        String ip = getIpAddress(request);
        log.info("【配置中心登录】【{}】注销", ip);
        request.getSession().invalidate();
        return "login";
    }

    /**
     * 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址
     *
     * @param request
     * @return
     */
    private static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (String strIp : ips) {
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

}
